<template lang="html">
  <div class="container">
    <el-form ref="form" :model="form" label-width="150px" @submit.native.prevent>
        <el-form-item label="所属门店Id">
            <el-input v-model="form.storeId" style="width:200px"/></el-input>
        </el-form-item>
        <el-form-item label="账号名">
            <el-input v-model="form.userName" style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="用户真实姓名">
            <el-input v-model="form.realName" style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="账号密码">
            <el-input v-model="form.password" style="width:200px"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
            <el-input v-model="form.mobile" style="width:200px"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">添加账户</el-button>
        </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        storeId: '',
        userName: '',
        realName: '',
        password: '',
        mobile: '',
        
      }
    }
  },
  methods: {
    onSubmit (){
        var reg = /^1[3456789]\d{9}$/
        if(this.form.storeId ==""){
            this.$message.error('请输入用户所属门店Id');
            return false;
        }
        else if(this.form.userName ==""){
            this.$message.error('请输入账户名');
            return false;
        }
        else if(this.form.realName ==""){
            this.$message.error('请输入用户真实姓名');
            return false;
        }
        else if(this.form.mobile == "" ){
            this.$message.error('手机号码不能为空')
            return false;
        }
        else if(this.form.realName ==""){
            this.$message.error('请输入用户真实姓名');
            return false;
        }
        // 验证手机号码是否匹配
        else if (!reg.test(this.form.mobile)) {
          this.$message.error('请输入正确的手机号码')
          return false;
        }
        else{
          const param =
          {
            storeId: parseInt(this.form.storeId) ,
            userName: this.form.userName,
            realName: this.form.realName,
            password: this.form.password,
            mobile: this.form.mobile
          }
         this.addStore(param);
        }
      },
      addStore (param){
          var _this = this
          const querystring = require('querystring');
          this.$axios.post('/manage/store/account/add',querystring.stringify(param)).then(response=>{
              //console.log(response);
              if(response.data.code==200){
                  this.$message({
                      message: '账户添加成功',
                      type: 'success'
                  });
                  setTimeout(function(){
                    _this.$router.push({path:"/store/account"})
                  },1500);

              }
              else{
                console.log(response.data.code)
                  this.$notify.error({
                      title: '添加失败',
                      duration:2000,
                      message: '失败原因：'+response.data.msg
                  });
              }

          }).catch(error=>{
              console.log(error);
          });
      },
  }
}
</script>

<style lang="css">
</style>
